package com.flashflexpro.flexdirector.adapter.descriptor{
    import com.flashflexpro.flexdirector.adapter.CodecHelper;
    import com.flashflexpro.flexdirector.adapter.codec.ICodec;

    import flash.events.Event;

    import mx.automation.Automation;
    import mx.automation.AutomationEventDescriptor;
    import mx.automation.IAutomationEventDescriptor;
    import mx.automation.IAutomationObject;
    import mx.automation.events.AutomationReplayEvent;

    public class DescEvent extends AutomationEventDescriptor implements IAutomationEventDescriptor{
        public function DescEvent( name:String, eventClassName:String, eventType:String, args:Array ){
            super( name, eventClassName, eventType, args );
            _descPropertyArr = args;
        }


        override public function record( target:IAutomationObject, event:Event ):Array{
            var rt:Array = [];
            for( var i:int = 0; i < _descPropertyArr.length; i ++ ){
                var pd:DescPropertyAuto = _descPropertyArr[ i ] as DescPropertyAuto;
                var codec:ICodec = CodecHelper.getCodec( pd.codecName );
                rt.push( codec.encode( event, pd, target ) );
            }
            return rt;
        }


        override public function replay( target:IAutomationObject, args:Array ):Object{
            var event:Event = createEvent( target );
            for( var i:int = 0; i < _descPropertyArr.length; i ++ ){
                var property:DescPropertyAuto = _descPropertyArr[ i ] as DescPropertyAuto;
                var codec:ICodec = CodecHelper.getCodec( property.codecName );
                codec.decode( event, args[ i ], property, target );
            }
            var re:AutomationReplayEvent = new AutomationReplayEvent();
            re.automationObject = target;
            re.replayableEvent = event;
            Automation.automationManager.replayAutomatableEvent( re );
            return null;
        }

        private var _descPropertyArr:Array;

        override public function getArgDescriptors( obj:IAutomationObject ):Array{
            return _descPropertyArr;
        }


    }
}